﻿using FluentEmail.Core;
using FluentEmail.Smtp;
using furion.blog.Application.Contracts;
using furion.blog.Domain.Mongo;
using furion.blog.Domain.Shared.Dto.Email;
using furion.blog.Domain.Shared.Enum;
using furion.blog.Utils;
using Furion;
using Furion.DependencyInjection;
using MongoDB.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;

namespace furion.blog.Application
{
    public class EmailService : IEmailService, ISingleton
    {
     

        public EmailService(){
            
        }

        /// <summary>
        /// 邮件发送
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task Send(EmailSendDto dto) {
            await EmailUtil.Send(dto.To, dto.Title, dto.Content, (model) => {
                //发送成功
                DB.SaveAsync(new EmailSendLog { 
                    ID = IdUtil.Id().ToString(),
                    Subject = model.Title,
                    Content = model.Content,
                    From = model.From,
                    To = model.To,
                    Cc = model.Cc,
                    Result = SuccessOrFailEnum.Success.ToString(),
                    Message = string.Empty,
                });
            }, (model, errors) => {
                //发送失败
                DB.SaveAsync(new EmailSendLog
                {
                    ID = IdUtil.Id().ToString(),
                    Subject = model.Title,
                    Content = model.Content,
                    From = model.From,
                    To = model.To,
                    Cc = model.Cc,
                    Result = SuccessOrFailEnum.Fail.ToString(),
                    Message = errors,
                });
            });
        }
    }
}
